package com.htc.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class WebService {

	private static WebService instance;

	private static String URL = "http://dev.duleaf.com/dlc/index.php/dlcwebservices/";

//	 private static String URL =
//	 "http://approxen.com/dlc/index.php/dlcwebservices/";
	// private static String URL =
	// "http://192.168.1.5/dwe/index.php/dwewebservices/";
	// private static String URL =
	// "http://192.168.1.2/dwe/index.php/dwewebservices/";
	// private static String URL =
	// "http://192.168.1.7:8888/dwe/index.php/dwewebservices/";
	// private static String URL =
	// "http://192.168.0.113:8888/dwe/index.php/dwewebservices/";

	public static WebService getInstance() {
		if (instance == null)
			instance = new WebService();
		return instance;
	}

	/**
	 * For UpdateData:
	 * UpdateData/tblname/user/type/1/username/p/password/abc/phone
	 * /123/country/vn For GetData: GetData/service/get_log/change_log_id/0
	 * 
	 * @param path
	 * @return
	 */
	public String get(String path) {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			String urlPath = URL + path;
			HttpGet getRequest = new HttpGet(urlPath);
			getRequest.addHeader("accept", "application/json");

			HttpResponse response = httpClient.execute(getRequest);

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(response.getEntity().getContent())));

			String output;
			System.out.println("Output from Server .... \n");
			StringBuilder builder = new StringBuilder();
			while ((output = br.readLine()) != null) {
				System.out.println(output);
				builder.append(output);
			}

			httpClient.getConnectionManager().shutdown();

			return builder.toString();

		} catch (ClientProtocolException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();
		}

		return null;
	}
	
	public String getWheatherAPI(String path) {
		try {

			DefaultHttpClient httpClient = new DefaultHttpClient();
			String urlPath = path;
			HttpGet getRequest = new HttpGet(urlPath);
			getRequest.addHeader("accept", "application/json");

			HttpResponse response = httpClient.execute(getRequest);

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(response.getEntity().getContent())));

			String output;
			System.out.println("Output from Server .... \n");
			StringBuilder builder = new StringBuilder();
			while ((output = br.readLine()) != null) {
				System.out.println(output);
				builder.append(output);
			}

			httpClient.getConnectionManager().shutdown();

			return builder.toString();

		} catch (ClientProtocolException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();
		}

		return null;
	}

}
